package at.ac.lbg.media.vis.client.theme.render
{
	import at.ac.lbg.media.vis.client.theme.view.Artwork;
	import at.ac.lbg.media.vis.client.theme.view.ArtworkStack;
	import at.ac.lbg.media.vis.client.theme.view.Category;
	
	import flare.util.Colors;
	import flare.util.Shapes;
	import flare.vis.data.DataSprite;
	import flare.vis.data.render.IRenderer;
	
	import flash.display.Graphics;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.core.Application;

	public class ArtworkRenderer implements IRenderer {
		private static var _instance:ArtworkRenderer = new ArtworkRenderer();
		/** Static ArtworkRenderer instance. */
		public static function get instance():ArtworkRenderer { return _instance; }
		
		/** The default size value for drawn shapes. This value is multiplied
		 *  by a DataSprite's size property to determine the final size. */
		public var defaultSize:Number = 6;
		
		private var catStructure:ArrayCollection;
		private var selectedCats:ArrayCollection;
		private var mainCat:Category;
		private var wedgeDegrees:Number;
		private var wedgeRadian:Number;
		private var startRadian:Number;
		private var endRadian:Number;
		private var color1:uint;
		private var color2:uint;
		private var cat:Category;
		private var innerCircleSize:int = 7;
		private var emptyColor:uint;
		private var colorActive:uint;
		private var colorInactive:uint;
		private var colorRange:ArrayCollection = new ArrayCollection();
		private var colorRange2:ArrayCollection = new ArrayCollection();
		public var stacks:ArrayCollection = new ArrayCollection();

		
		public function ArtworkRenderer() {
			catStructure = Application.application.catStructure;
			selectedCats = Application.application.selectedCats2;
			colorRange = Application.application.colorRange;
			colorRange2 = Application.application.colorRange2;
		}
		
		/** @inheritDoc */
		public function render(d:DataSprite):void {
			if (!selectedCats) {
				return;
			}
			var artwork:Artwork = d as Artwork;	
			renderThemes2(artwork);
		}
		
		private function renderThemes2(artwork:Artwork):void {
			var g:Graphics = artwork.graphics;
			g.clear();

			artwork.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			
			var catsNum:int = selectedCats.length;
			var diskSize:int = 14;
			wedgeDegrees = 360 / catsNum;
			wedgeRadian = Math.PI / 180 * wedgeDegrees;
			startRadian = 0;
			endRadian = wedgeRadian;
			colorActive = Colors.rgba(230, 130, 30);
			colorInactive = Colors.rgba(220, 220, 220);
			var xOffset:int = 0;
			var yOffset:int = 0;
			
			
			
			// stackCount is computed on the server and indicates the amount
			// of artworks on a stack. we do only draw a stack once and skip all
			// the other artworks on this stack.
			if (artwork.stackCount > 1) {
				var c:Number = artwork.origX + artwork.origY;
				for each (var st:ArtworkStack in stacks) {
					if (st.coords == c) {
						st.label += "\n" + artwork.title;
						return;
					}
				}
				// we built a new stack...
				var stack:ArtworkStack = new ArtworkStack();
				stack.coords = c;
				stack.displayedArtwork = artwork;
				stack.label = artwork.title;
				xOffset = artwork.stackCount;
				yOffset = artwork.stackCount;
				renderStack(artwork, diskSize);
				
				stacks.addItem(stack);
			}
			if (artwork.highlightMe > 0) {
				g.lineStyle(1, Colors.darker(colorInactive, 1), 1);
			} else {
				
				g.lineStyle(1, colorInactive, 1);
			}
			
			// draw one wedge per category
			for (var i:int = 0; i < catsNum; i++) {
				mainCat = selectedCats[i];
				
				if (artwork.categoryIds.contains(mainCat.id)) {
					if (artwork.highlightMe == mainCat.id) {
						g.beginFill(Colors.rgba(254, 254, 254), 1);
					} else {
						g.beginFill(colorActive, 1);
					}
				} else {
					if (artwork.highlightMe > 0) {
						g.beginFill(Colors.darker(colorInactive, 1), 1);
					} else {
						g.beginFill(colorInactive, 1);
					}
				}
				Shapes.drawWedge(g, 0, -yOffset, diskSize, 0, startRadian, endRadian);
				startRadian += wedgeRadian;
				endRadian += wedgeRadian;
			}
			g.endFill();
			// Label
//			var label:TextSprite = new TextSprite(artwork.title);
//			label.y = -30;
//			artwork.addChild(label);
		}
		
		public function onMouseOver(event:MouseEvent):void {
			if (event.target is Artwork) {
				var artwork:Artwork = Artwork(event.target);
				stacks.removeAll();
	            artwork.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
	            Application.application.legendRenderer.redraw(artwork.categoryIds);
	  		}
		}
		
		private function onMouseOut(event:MouseEvent):void {
			stacks.removeAll();
			Application.application.legendRenderer.redraw();
		}
		
		private function renderStack(artwork:Artwork, diskSize:int):void {
			artwork.graphics.lineStyle(2, Colors.darker(colorInactive, 0.5), 1);
			for (var i:int = 0; i < artwork.stackCount; i++) {
				artwork.graphics.drawCircle(0, -i, diskSize);
			}
			
		}
		
//		private function renderThemes(artwork:Artwork):void {
//			var g:Graphics = artwork.graphics;
//			g.clear();
//			//g.lineStyle(1, Colors.rgba(0, 0, 0), lineAlpha);
//
//			emptyColor = Colors.rgba(255, 255, 255);
//			//var mainCatsNum:uint = selectedCats.length;
//			var mainCatsNum:uint = 1;
//			var diskSize:int = 5;
//			var startDisk:int;
//			var endDisk:int;
//
//			
//			// first the cats that are unset. drawing from outer to inner circle
//			for (var k:int = 0; k < mainCatsNum; k++) {
//				var count:int = mainCatsNum - 1 - k;
//				mainCat = selectedCats[k];
//				wedgeDegrees = 360 / mainCat.children.length;
//				wedgeRadian = Math.PI / 180 * wedgeDegrees;
//				startRadian = 0;
//				endRadian = wedgeRadian;
//				//color1 = colorRange.getItemAt(mainCat.index) as uint;
//				//color2 = colorRange2.getItemAt(mainCat.index) as uint;
//
//				startDisk = count * diskSize + innerCircleSize;
//				endDisk = startDisk + diskSize;
//				
//				if (k == mainCatsNum - 1) {
//					startDisk = 0;
//				}
//	
//				for (var i:int = 0; i < mainCat.children.length; i++) {
//					color = Colors.rgba(190, 190, 190, 1);
//					cat = mainCat.children[i] as Category;
//					if (artwork.highlightMe > 0) {
//						color = Colors.darker(color, 2);
//						g.beginFill(color, 0.4);
//					} else {
//						g.beginFill(color, 0.2);
//					}
//					
//					Shapes.drawWedge(g, 0, 0, endDisk, startDisk, startRadian, endRadian);
//					
//					startRadian += wedgeRadian;
//					endRadian += wedgeRadian;
//				}
//				
//			}
//			
//			// now the active categories
//			for (k = 0; k < mainCatsNum; k++) {
//				count = mainCatsNum - 1 - k;
//				mainCat = selectedCats[k];
//				wedgeDegrees = 360 / mainCat.children.length;
//				wedgeRadian = Math.PI / 180 * wedgeDegrees;
//				startRadian = 0;
//				endRadian = wedgeRadian;
//				color1 = colorRange.getItemAt(mainCat.index) as uint;
//				color2 = colorRange2.getItemAt(mainCat.index) as uint;
//				var fade:Number = 0.7 + 0.1 * k;
//
//				startDisk = count * diskSize + innerCircleSize;
//				endDisk = startDisk + diskSize;
//				
//				if (k == mainCatsNum - 1) {
//					startDisk = 0;
//				}
//
//				for (i = 0; i < mainCat.children.length; i++) {
//					//color = i % 2 == 0? color1 : color2;
//					color = color1;
//					cat = mainCat.children[i] as Category;
//					if (artwork.categoryIds.contains(cat.id)) {
//						if (artwork.highlightMe == cat.id) {
//							//color = Colors.brighter(color, 3);
//							color = Colors.rgba(254, 254, 254);
//							g.beginFill(color, 1);
//						} else {
//							g.beginFill(color, fade);
//						}
//						
//						//GraphicsUtil.drawWedge(g, 0, 0, endDisk, startDisk, startRadian, endRadian);
//						Shapes.drawWedge(g, 0, 0, endDisk, 0, startRadian, endRadian);
//					}
//					startRadian += wedgeRadian;
//					endRadian += wedgeRadian;
//				}
//				
//				
//			}
//			
//			g.endFill();
//
//		}
	
		
	}
}